/**
 * 基于BootstrapTable表格封装
 *
 * @lastmodidy 2015-10-05
 */
!function($, window){

    var FireTable = function(options){
        this.table = $(options.table);

        delete options.table;

        this.initFilter( options );

        this.initOptions( options );

        //渲染表格
        this.table.bootstrapTable( this.options );
    }

    //默认配置
    FireTable.defaultOptions = {
        columns:[],
        url:'',
        cache:false,
        contentType:'application/x-www-form-urlencoded',
        pagination:true,
        sidePagination:'server',
        pageSize:20,
        pageNumber:1,
        pageList:[20, 40, 60, 80],
        method:'get',
        singleSelect:true,
        clickToSelect:true,
        sortName:'id',
        sortOrder:'desc',
        classes:'table',
        formatLoadingMessage:function() {
            return '载入中请稍候……';
        },
        formatShowingRows:function(from, to, rows) {
            return '当前第 '+ from +' 条,到 '+ to +' 条,总条数 '+ rows +' ';
        }
    };

    FireTable.prototype = {
        loading:false,
        options:{},
        lastQuery:'',
        // 绑定筛选表单
        initFilter:function( options ) {

            var that = this;

            if( options.filter != undefined ){
                this.filter = $(options.filter);
            } else {
                this.filter = $('<form id="bst-filter-'+ new Date().getMilliseconds() +'" style="display: none;"></form>');
                $('body').append(this.filter);
            }

            this.filter.on('submit.bootstarp-table', function() {
                that.loadData();
                return false;
            });
        },
        initOptions:function( options ){

            var that = this,
                isInit = false;

            //检测页数缓存表单
            var isInitControl = this.filter.find('[name=is-init]');
            if( isInitControl.length != 0 ){
                isInit = isInitControl.val() == '1'
            }


            if(!isInit){

                //检测页数缓存表单
                var pageNumberControl = this.filter.find('[name=page-number]');

                if( pageNumberControl.length != 0 ){
                    //读取分页
                    options.pageNumber = parseInt( pageNumberControl.val() );
                }

                var pageSizeControl = this.filter.find('[name=page-size]');
                if( pageSizeControl.length != 0 ) {
                    //读取页栏
                    options.pageSize = parseInt( pageSizeControl.val() );
                }

            }

            //加载数据前
            options.queryParams = function(params) {
                that.loading = true;
                return that.beforeQuery(params);
            };

            //数据加载成功后
            options.onLoadSuccess = function() {
                that.loading = false;
                return true;
            };

            //数据加载失败后
            options.onLoadError = function() {
                that.loading = false;
                alert('服务端繁忙,数据加载错误!');
                return true;
            };

            //换页后
            options.onPageChange = function(number, size) {
                that.cachePager(number, size);
            };

            this.options = $.extend({}, FireTable.defaultOptions, options);
        },
        beforeQuery:function(params) {
            var query = this.filter.serialize(), i;

            for(i in params){
                if(params[i] == undefined)
                    continue;

                query = query+'&'+i+'='+params[i];
            }

            this.lastQuery = query;

            return query;
        },
        cachePager:function(number, size) {

            //检测页数缓存表单
            var isInitControl = this.filter.find('[name=is-init]');
            if( isInitControl.length != 0 ){
                isInitControl.val(0);
            }

            var pageNumberControl = this.filter.find('[name=page-number]');

            if( pageNumberControl.length != 0 ){
                //缓存分页
                pageNumberControl.val(number);
            }

            var pageSizeControl = this.filter.find('[name=page-size]');
            if( pageSizeControl.length != 0 ) {
                //缓存页栏
                pageSizeControl.val(size);
            }
        },
        loadData:function() {
            this.table.bootstrapTable('refresh');
        },
        getSelections:function() {

            return this.table.bootstrapTable('getSelections');
            var muti = this.table.bootstrapTable('getSelections');
        },
        getCurrent:function() {
            var muti = this.table.bootstrapTable('getSelections');
            if(muti.length){
                return muti[0];
            }else{
                return false;
            }
        },
        getData:function(index) {

            return this.table.bootstrapTable('getData')[index];
        },
        reload:function() {

            if( this.loading ) return;

            this.loadData();
        }
    }

    FireTable.init = function(options){

        if(options.table == undefined){
            alert('必须传入渲染的表格');
            return false;
        }

        if(options.columns == undefined){
            alert('必须传入列配置');
            return false;
        }

        if(options.url == undefined){
            alert('必须传入数据的表格');
            return false;
        }

        return new FireTable(options);
    }

    window.FireTable = FireTable;

}(jQuery, window);